// index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    st:0,//0:展示用户可使用的优惠券,1:展示订单可使用的优惠券,2:展示领券中心
    couponList: [],//用户可使用的优惠券
    validCouponList:[],//订单可使用的优惠券
    typeList:[],//优惠券类型列表
    selList: ''//选中的购物车项键列表
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that=this;
    var st=0;
    if(e.st){
      st = e.st;
    }
    if(e.sl){
      that.setData({ selList:e.sl});
    }
    that.setData({st:st});
  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    var that=this;
    if(that.data.st==0){
      that.GetUserCouponList();
    }else if(that.data.st==1){
      that.GetOrderCouponList();
    }
    else if(that.data.st==2){
      that.GetCouponTypeList();
    }
  },
  /**
   * 获取用户可以使用的优惠券
   */
  GetUserCouponList: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/user/CouponList',
      data: {
        uid: app.globalData.userInfo.Uid,
        type: 1
      },
      success: function (res) {
        console.log('优惠券1');
        console.log(res);
        if (res.data.Code == 0) {
          var couPonList = res.data.Data.CouponList
          for (var i = 0; i < couPonList.length;i++){
            couPonList[i].usestarttime = couPonList[i].usestarttime.slice(0,10)
            couPonList[i].useendtime = couPonList[i].useendtime.slice(0, 10)
          }
          that.setData({
            couponList: res.data.Data.CouponList
          })
        }
      }
    })
  },
  /**
   * 获取订单可使用的优惠券
   */
  GetOrderCouponList: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/order/GetValidCouponList',
      data: {
        uid: app.globalData.userInfo.Uid,
        sellist: that.data.selList
      },
      success: function (res) {
        console.log(res);
        console.log("获取订单可使用的优惠券2");
        if (res.data.Code == 0) {
          that.setData({
            validCouponList: res.data.Data
          })
        }
      }
    })
  },
  GetCouponTypeList: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/store/GetSendingCouponTypeList',
      data: {
        storeid: app.globalData.storeId
      },
      success: function (res) {
        console.log("优惠券3");
        console.log(res);
        if (res.data.Code == 0) {
          var couPonList = res.data.Data
          for (var i = 0; i < couPonList.length; i++) {
            couPonList[i].UseStartTime = couPonList[i].UseStartTime.slice(0, 10)
            couPonList[i].UseEndTime = couPonList[i].UseEndTime.slice(0, 10)
          }
          that.setData({
            typeList: res.data.Data
          })
        }
      }
    })
  },
  selectCoupon:function(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    var c = that.data.validCouponList[index];
    wx.setStorageSync(that.data.selList, c);
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 领取优惠券
   */
  getCoupon:function(e){
    var that = this;
    var index=e.currentTarget.dataset.index;
    var couponType = that.data.typeList[index];
    wx.request({
      url: app.globalData.host + '/api/coupon/GetCoupon',
      data: {
        couponTypeId: couponType.CouponTypeId,
        uid:app.globalData.userInfo.Uid
      },
      success: function (res) {
        console.log(res);
        if (res.data.Code == 0) {
          wx.showModal({
            title: '提示',
            content: '领取成功',
            showCancel:false
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})